/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.appschool.action;

import br.com.appschool.dao.DisciplinaDAO;
import br.com.appschool.dao.HorarioAulasDAO;
import br.com.appschool.dao.ProfessorDAO;
import br.com.appschool.model.Disciplina;
import br.com.appschool.model.HorarioAulas;
import br.com.appschool.model.Professor;
import br.com.appschool.srv.HorarioAulasSV;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author joaopaulo
 */
public class HorarioAulasAction {

    public void execute(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {


        String action = request.getParameter("action");

        if (action.equals("novo")) {
            request.setAttribute("objHorarioAulas", new HorarioAulas());
            request.getRequestDispatcher("cadastroHorarioAulas.jsp").forward(request, response);
            return;
        }
        if (action.equals("listar")) {
            try {
                request.setAttribute("lstHorarioAulass", new HorarioAulasDAO().findAll("HorarioAulas", "nome"));
                request.getRequestDispatcher("lsthorarioAulas.jsp").forward(request, response);
            } catch (Exception ex) {
                Logger.getLogger(HorarioAulasSV.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (action.equals("salvar")) {
            if (this.save(request)) {
                request.setAttribute("msg", "Operacao realizada com sucesso!");
            } else {
                request.setAttribute("msg", "Erro ao realizar a operação!");
            }

            request.setAttribute("objHorarioAulas", new HorarioAulas());
            request.getRequestDispatcher("cadastroHorarioAulas.jsp").forward(request, response);
            return;
        }
        if (action.equals("excluir")) {
            try {

                int id = Integer.parseInt(request.getParameter("codigo"));
                new HorarioAulasDAO().delete(HorarioAulas.class, id);

                request.setAttribute("lstHorarioAulass", new HorarioAulasDAO().findAll("HorarioAulas", "nome"));
                request.getRequestDispatcher("lsthorarioAulas.jsp").forward(request, response);

            } catch (Exception ex) {
                Logger.getLogger(HorarioAulasSV.class.getName()).log(Level.SEVERE, null, ex);
            }
            return;
        }


        if (action.equals("editar")) {
            try {
                int id = Integer.parseInt(request.getParameter("codigo"));

                request.setAttribute("objHorarioAulas", new HorarioAulasDAO().findById(HorarioAulas.class, id));
                request.getRequestDispatcher("cadastroHorarioAulas.jsp").forward(request, response);

            } catch (Exception ex) {
                Logger.getLogger(HorarioAulasSV.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

    }

    private boolean save(HttpServletRequest request) {

        HorarioAulas horarioAulas = new HorarioAulas();
        int idDisciplina = Integer.parseInt(request.getParameter("idDisciplina"));
        int idProfessor = Integer.parseInt(request.getParameter("idProfessor"));


        horarioAulas.setId(Integer.parseInt(request.getParameter("id")));
        horarioAulas.setDiaSemana(Integer.parseInt(request.getParameter("diaSemana")));
        horarioAulas.setIdDisciplina(new DisciplinaDAO().findById(Disciplina.class, idDisciplina));
        horarioAulas.setIdProfessor(new ProfessorDAO().findById(Professor.class, idProfessor));
        horarioAulas.setSala(request.getParameter("sala"));

        try {
            if (horarioAulas.getId() == 0) {
                return new HorarioAulasDAO().create(horarioAulas);
            } else {
                return new HorarioAulasDAO().update(horarioAulas);
            }
        } catch (Exception error) {
            return false;
        }
    }
}
